﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MoreMountains.Tools;

/*
 * 1.粒子特效
 * 2.音效
 * 3.飘血
 */



/// <summary>
/// 用于显示战斗的类
/// </summary>
public class BattleDisplay : MMSingleton<BattleDisplay>
{
    public GameObject characterDisplayPrefab;
    public Transform playerSpawnPos;
    public Transform enemySpawnPos;

    List<GameObject> playerTeam;
    List<GameObject> enemyTeam;
    
    /// <summary>
    /// 运行显示战斗
    /// </summary>
    public void RunDisplay()
    {
        SwapTeam(playerSpawnPos, BattleManager.Instance.playerT, true,out playerTeam);
        SwapTeam(enemySpawnPos, BattleManager.Instance.enemyT, false,out enemyTeam);
        StartCoroutine(DisplayBattle());
    }

    IEnumerator DisplayBattle()
    {
        foreach (var i in BattleManager.Instance.attackInfoList)
        {
            print(i);

            GameObject attackObj = GetDisplayObjectWithID(i.attacker.id);
            GameObject targetObj = GetDisplayObjectWithID(i.attackTarget.id);
            CharacterDisplay charDisp = attackObj.GetComponent<CharacterDisplay>();
            float attackTime = charDisp.Attack(i.attackID);
            yield return new WaitForSeconds(attackTime);
            charDisp.ShowAttackEffect(i.attackID, targetObj);
            yield return new WaitForSeconds(0.5f);

            charDisp = targetObj.GetComponent<CharacterDisplay>();
            charDisp.Hit();
            if (i.targetCurrentAlive== false)
            {
                charDisp.Die();
            }
            yield return new WaitForSeconds(1f);
        }
        yield return null;
    }

    GameObject GetDisplayObjectWithID(int id)
    {
        
        GameObject go = playerTeam.Find(s => s.GetComponent<CharacterDisplay>().id == id);
        if (go)
        {
            return go;
        }
        else {
            go = enemyTeam.Find(s => s.GetComponent<CharacterDisplay>().id == id);
            return go;
        }
    }

    /// <summary>
    /// 生成角色
    /// </summary>
    /// <param name="spawnPos"></param>
    /// <param name="team"></param>
    /// <param name="flipx"></param>
    void SwapTeam(Transform spawnPos,List<Character> team,bool flipx,out List<GameObject> saveTeam)
    {
        int index = 0;
        saveTeam = new List<GameObject>();
        foreach (var i in team)
        {
            Transform pos = spawnPos.GetChild(index);
            GameObject characterObject = Instantiate(characterDisplayPrefab, pos);
            characterObject.name = i.name;
            saveTeam.Add(characterObject);
            SpriteRenderer sr = characterObject.GetComponent<SpriteRenderer>();
            sr.sprite = BattleResRef.Instance.GetSpriteByName(i.name);
            sr.flipX = flipx;

            CharacterDisplay charDis = characterObject.GetComponent<CharacterDisplay>();
            charDis.id = i.id;
            charDis.animator.runtimeAnimatorController = i.animatorController;
            index++;
            index %= 3;
        }
    }
}
